/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：SystemLogDaoImpl.java
 * History:
 *       May 14, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.ISystemLogDao;
import com.xteam.ecsystem.entities.LogSystemLog;
import com.xteam.ecsystem.utils.JsonDateValueProcessor;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 系统日志数据访问实现类
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class SystemLogDaoImpl extends DDLPrefix implements ISystemLogDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	/**
	 * @see com.xteam.ecsystem.dao.ISystemLogDao#delSystemLogsByIds(java.lang.String)
	 */
	public void delSystemLogsByIds(String ids) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.LogSystemLog");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	/**
	 * @see com.xteam.ecsystem.dao.ISystemLogDao#getSystemLogList(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public String getSystemLogList(int pageIndex, int pageSize, String lslUser,
			String lslAddress, String startTime, String endTime) {
		JSONObject jo = new JSONObject();
		
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + LogSystemLog.REF + SQL_WHERE +" 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + LogSystemLog.REF + SQL_WHERE +" 1=1 ";
			
			if (lslUser != null && lslUser.length() > 0
					&& !lslUser.equals("undefined")
					&& !lslUser.equals("null")) {
				hql += SQL_AND + " lslUser " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " lslUser " + SQL_LIKE + " ? ";
				list.add("%" + lslUser + "%");

			}
						
			if(lslAddress != null && lslAddress.length() > 0
					&& !lslAddress.equals("undefined")
					&& !lslAddress.equals("null")){
				hql += SQL_AND + " lslAddress " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " lslAddress " + SQL_LIKE + " ? ";
				list.add("%" + lslAddress + "%");
			}
			
			if (startTime != null && startTime.length() > 0
					&& !startTime.equals("undefined")
					&& !startTime.equals("null")) {
				hql += " and lslDate >=? "; 
				countHQL += " and lslDate >=? ";
			    list.add(StringUtil.strToDate(startTime, null));
				 
			 }
			
			if (endTime != null && endTime.length() > 0
					&& !endTime.equals("undefined")
					&& !endTime.equals("null")) {
				hql += " and lslDate <=? "; 
				countHQL += " and lslDate <=? ";
			    list.add(StringUtil.strToDate(endTime, null));
			 }
			
			hql += SQL_ORDERBY + "lslDate desc";

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, list.toArray(), 
							pageIndex * pageSize, pageSize);
	
			// 率先格式化日期，在取数据时便不必再作处理。
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			
			jo = JSONObject.fromObject(p, jsonConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	/**
	 * @see com.xteam.ecsystem.dao.ISystemLogDao#saveSystemLogs(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void saveSystemLogs(String lslUser, String lslAddress,
			String lslFunction, String lslMethod, String lslType,
			String lslObject) {
		LogSystemLog systemLog = new LogSystemLog();
		systemLog.setLslFunction(lslFunction);
		systemLog.setLslMethod(lslMethod);
		systemLog.setLslType(lslType);
		systemLog.setLslObject(lslObject);
		systemLog.setLslUser(lslUser);
		systemLog.setLslAddress(lslAddress);
		systemLog.setLslDate(new Date());		
		
		this.baseDao.save(systemLog);
	}
	
	public void delSystemLogByCon(String condition){
		String hql = "";
		
		try {
			if (condition != null && !"".equals(condition) 
					&& !"null".equals(condition) && !"undefined".equals(condition)) {
				int type = Integer.parseInt(condition);
				switch (type) {
				case 0: // 一周之前
					hql = SQL_DELETE + SQL_FROM + LogSystemLog.REF + SQL_WHERE 
							+ "DATEDIFF(NOW(), lslDate) > 7";  
					break;
				case 1: // 一个月之前
					hql = SQL_DELETE + SQL_FROM + LogSystemLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lslDate) > 30"; 
					break;
				case 2: // 三个月之前
					hql = SQL_DELETE + SQL_FROM + LogSystemLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lslDate) > 90"; 
					break;
				case 3: // 半年之前
					hql = SQL_DELETE + SQL_FROM + LogSystemLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lslDate) > 180"; 
					break;
				case 4: // 一年之前
					hql = SQL_DELETE + SQL_FROM + LogSystemLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lslDate) > 360"; 
					break;
				default:
					break;
				}
				
				this.baseDao.executeUpdate(hql);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getLastLogTime(String lslUser){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String lastLogTime = sdf.format(new Date());
		String lslMethod = "login";
		
		try {
			String hql = SQL_FROM + LogSystemLog.REF + SQL_WHERE + "lslUser=?" + SQL_AND + "lslMethod=?" 
					   + SQL_ORDERBY + "lslDate" + SQL_DESC;
			Object obj = this.baseDao.findObject(hql, new Object[]{lslUser, lslMethod});
			
			if (obj != null) {
				LogSystemLog lsl = (LogSystemLog)obj;
				lastLogTime = sdf.format(lsl.getLslDate());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return lastLogTime;
	}

}
